package com.jp.knowledge.my.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.support.annotation.NonNull;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.resource.drawable.GlideDrawable;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.animation.GlideAnimation;
import com.bumptech.glide.request.target.Target;
import com.bumptech.glide.request.target.ViewTarget;
import com.google.gson.JsonObject;
import com.jiayen.util.ToasUtil;
import com.jp.knowledge.R;
import com.jp.knowledge.http.HttpHelper;
import com.jp.knowledge.model.IModel;
import com.jp.knowledge.model.ProductUIModel;
import com.jp.knowledge.my.base.RecycleVAdapter;
import com.jp.knowledge.my.base.RecycleVHolder;
import com.jp.knowledge.my.model.CollectionUiModel;
import com.jp.knowledge.util.ImageLoadUtil;
import com.jp.knowledge.util.ResultSubscriber;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by 528787067@qq.com on 2017/1/21.
 */

public class UiFragmentAdapter extends RecycleVAdapter<ProductUIModel> implements ResultSubscriber.OnResultListener, View.OnClickListener {

    private boolean isIcon;
    protected static final int COLLECTION_OPERATE_CODE = 0x01;

    public UiFragmentAdapter(Context context, @NonNull List<ProductUIModel> list) {
        this(context, list, false);
    }

    public UiFragmentAdapter(Context context, @NonNull List<ProductUIModel> list, boolean isIcon) {
        super(context, list);
        this.isIcon = isIcon;
    }

    @Override
    public int getLayoutId(int viewType) {
        return R.layout.fragment_collection_ui;
    }

    @Override
    public void onBind(RecycleVHolder holder, final int position) {
        final CollectionUiModel uiModel = getData(position);

        final FrameLayout frameLayout = holder.get(R.id.ui_img_frame);
        final ImageView imageView = holder.getImageView(R.id.ui_img);
//        imageView.getLayoutParams().width = frameLayout.getMeasuredWidth();
//        imageView.getLayoutParams().height = frameLayout.getMeasuredWidth();
        imageView.setScaleType(ImageView.ScaleType.CENTER);
        imageView.setImageResource(R.mipmap.morentutouxiang);
        Glide.with(this.mContext)
//            .load(uiModel.getCover()+"?imageView2/2/w/" + imageView.getMeasuredWidth() + "/interlace/1/format/jpg")
            .load(uiModel.getCover()+"?imageMogr2/format/jpg/quality/75/thumbnail/!100p")//列表缩略图
            .asBitmap()
            .diskCacheStrategy(DiskCacheStrategy.SOURCE)
            .into(new ViewTarget<View, Bitmap>(imageView){
                @Override
                public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {
                    if(resource != null && !resource.isRecycled()) {
                        int itemWidth = frameLayout.getMeasuredWidth();
                        ViewGroup.LayoutParams imageLayoutParam = imageView.getLayoutParams();
                        if(isIcon || uiModel.getIsIcon() == 1) {
                            imageLayoutParam.width = itemWidth/3;
                            imageLayoutParam.height = itemWidth/3;
                            frameLayout.getLayoutParams().height = itemWidth;
                            imageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
                        } else{
                            imageLayoutParam.width = itemWidth;
                            imageLayoutParam.height = itemWidth * resource.getHeight() / resource.getWidth();
                            frameLayout.getLayoutParams().height = imageLayoutParam.height;
                            imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
                        }
                        imageView.setImageBitmap(resource);
                    }
                }
            });

//        ImageView imageView = holder.getImageView(R.id.ui_img);
//        int width = mContext.getResources().getDisplayMetrics().widthPixels/2;
//        int height = uiModel.getHeight() * (width / uiModel.getWidth());
//        imageView.getLayoutParams().height = height;
//        ImageLoadUtil.load(mContext, uiModel.getCover()+"?imageMogr2/format/jpg/quality/75/thumbnail/!30p", imageView);

        holder.setTextView(R.id.ui_title, uiModel.getName());
        if(uiModel.getIsFollow() == 1){
            holder.setImageResource(R.id.ui_collection, R.mipmap.kuaitiyanyishoucang);
        } else{
            holder.setImageResource(R.id.ui_collection, R.mipmap.kuaitiyanyishoucanghuise);
        }
        holder.get(R.id.ui_collection).setTag(position);
        holder.get(R.id.ui_collection).setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        int position = (int)v.getTag();
        CollectionUiModel uiModel = getData(position);
        JsonObject param = new JsonObject();
        param.addProperty("token", HttpHelper.token);
        param.addProperty("id", uiModel.getId());
        param.addProperty("type", uiModel.getType());
        if(uiModel.getIsFollow() == 1) {
            HttpHelper.getInstance(mContext).unCollection(param, position * 10 + COLLECTION_OPERATE_CODE, UiFragmentAdapter.this);
        } else{
            HttpHelper.getInstance(mContext).collectionHead(param, position * 10 + COLLECTION_OPERATE_CODE, UiFragmentAdapter.this);
        }
        uiModel.setIsFollow(uiModel.getIsFollow()==1 ? 0 : 1);
//        notifyDataSetChanged();
        notifyItemChanged(position);
    }

    @Override
    public void onStart(int requestType) {}
    @Override
    public void onCompleted(int requestType) {}
    @Override
    public void onError(int requestType) {
        if(requestType%10 != COLLECTION_OPERATE_CODE)
            return;
        CollectionUiModel uiModel = getData(requestType/10);
        uiModel.setIsFollow(uiModel.getIsFollow()==1 ? 0 : 1);
//        notifyDataSetChanged();
        notifyItemChanged(requestType/10);
        if(collectionCallback != null)
            collectionCallback.collectionOperateError(requestType/10);
    }
    @Override
    public void onNext(IModel data, int requestType) {
        if(data.getErrcode() != 0) {
            ToasUtil.toast(mContext, data.getMessage());
            onError(requestType);
        } else if(requestType%10 == COLLECTION_OPERATE_CODE){
            if(collectionCallback != null)
                collectionCallback.collectionOperateSuccess(requestType/10);
        }
    }

    private CollectionCallback collectionCallback;
    public void setCollectionCallback(CollectionCallback callback){
        this.collectionCallback = callback;
    }

    public interface CollectionCallback{
        void collectionOperateSuccess(int position);
        void collectionOperateError(int position);
    }
}
